package com.sxt.practiceapplication.utils.okhttp.url;

import java.util.HashMap;
import java.util.Map;

public class URL {
    private String url;

    private final Map<String, Object> params;

    @SafeVarargs
    public URL(String url, Param<String, Object>... params) {
        this.url = url;
        this.params = new HashMap<>();
        for (Param<String, Object> param : params) {
            this.params.put(param.key, param.value);
        }
    }

    public String urlStr() {
        if (!this.url.endsWith("/")) {
            this.url += "/";
        }
        StringBuilder url = new StringBuilder(this.url + "?");
        params.forEach((key, value) -> {
            url.append(key).append("=").append(value).append("&");
        });
        url.deleteCharAt(url.length() - 1);
        return url.toString();
    }
}
